/* 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <algorithm>
#include <QDateTime>
#include <QVariant>
#include <QtPlugin>
#include "hanabiraagent.h"
#include "log.h"
#include "components/chan.h"
#include "hanabiratasksendpost.h"
#include "tasks/taskboardrequest.h"
#include "tasks/taskthreadrequest.h"

namespace Wishmaster
{
	HanabiraAgent::HanabiraAgent(const QString& host) : DefaultAgent(host),
		m_host(host)
	{
	}

	HanabiraAgent::~HanabiraAgent()
	{
	}

	void HanabiraAgent::makePost(const PostDescriptor& post, const QString& captcha)
	{
		TaskPtr task = TaskPtr(new HanabiraTaskSendPost(post, captcha, requestManager(), "Sending post", this));
		connect(task.data(), SIGNAL(result(TaskSendPost*,QByteArray,QString)), this, SLOT(resultPost(TaskSendPost*,QByteArray,QString)));
		m_pending[task->id()] = post.parent_thread;
		taskhub()->queueTask(task);
	}

	void HanabiraAgent::createThread(const PostDescriptor& post, const QString& captcha)
	{
		TaskPtr task = TaskPtr(new HanabiraTaskSendPost(post, captcha, requestManager(), "Creating thread", this));
		connect(task.data(), SIGNAL(result(TaskSendPost*,QByteArray,QString)), this, SLOT(resultPost(TaskSendPost*,QByteArray,QString)));
		m_pending[task->id()] = post.parent_thread;
		taskhub()->queueTask(task);
	}

	void HanabiraAgent::requestThread(const ThreadId& threadId, const PostId& lastPostId)
	{
		QString reqlink;
		if(lastPostId.isValid() && (lastPostId.postId() > 0))
			reqlink = QString("%1res/%2.xml?last_post=%3").arg(threadId.boardId()).arg(threadId.threadNumericId()).arg(lastPostId.postId());
		else
			reqlink = QString("%1res/%2.xml").arg(threadId.boardId()).arg(threadId.threadNumericId());

		Wishmaster2::Network::Url url(("http://" + makeFullHostname(threadId.boardId()) + reqlink).toStdString());
		TaskThreadRequest* task = new TaskThreadRequest(threadId, url, tr("Requesting thread: %1").arg(threadId.threadNumericId()), requestManager());
		connect(task, SIGNAL(result(TaskThreadRequest*, QByteArray)), this, SLOT(slot_thread(TaskThreadRequest*, QByteArray)));
		TaskPtr taskptr = TaskPtr(task);
		taskhub()->queueTask(taskptr);
	}

	void HanabiraAgent::requestBoardIndex(const QString& boardId, int page)
	{
		Wishmaster2::Network::Url url(("http://" + makeFullHostname(boardId) + boardId + "index.xml").toStdString());
		TaskBoardRequest* task = new TaskBoardRequest(m_host, boardId, url, tr("Requesting board index: %1 (%2)").arg(boardId).arg(m_host), requestManager());
		connect(task, SIGNAL(result(TaskBoardRequest*, QByteArray)), this, SLOT(slot_board(TaskBoardRequest*, QByteArray)));
		TaskPtr taskptr = TaskPtr(task);
		taskhub()->queueTask(taskptr);
	}

	void HanabiraAgent::deleteThread(const ThreadId& threadId, const QString& password)
	{
	}

	void HanabiraAgent::deletePost(const PostId& postId, const QString& password)
	{
	}

	void HanabiraAgent::resultPost(TaskSendPost* task, const QByteArray& data, const QString& contentType)
	{
		auto it = m_pending.find(task->id());
		if(it == m_pending.end())
			return;
		ThreadId threadId = it->second;
		m_pending.erase(it);
		QString regexp = QString::fromUtf8("<center><h2>([^<>]*)</h2></center>");
		QRegExp rx(regexp);
		if(rx.indexIn(QString::fromUtf8(data)) != -1)
		{
			log(llDebug, "Error: %s", qPrintable(rx.cap(1)));
			emit postingError(rx.cap(1));
			return;
		}
		emit postingDone(threadId);
	}

	QStringList HanabiraAgent::contentRatings()
	{
		QStringList list;
		list.append("SFW");
		list.append("R-15");
		list.append("R-18");
		list.append("R-18G");
		return list;
	}

	ChanAgent* HanabiraAgent::clone(const QString& host)
	{
		return new HanabiraAgent(host);
	}

	QString HanabiraAgent::id()
	{
		return "hanabira";
	}
}

Q_EXPORT_PLUGIN2(hanabiraagent, Wishmaster::HanabiraAgent)
